package org.ubc.cs410.disposal2020.server;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.ubc.cs410.disposal2020.NoActiveGameException;
import org.ubc.cs410.disposal2020.Player;
import org.ubc.cs410.disposal2020.client.PlayerService;
import org.ubc.cs410.disposal2020.client.dto.RobotDto;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import org.ubc.cs410.disposal2020.*;

public class PlayerServiceImpl extends RemoteServiceServlet implements
PlayerService {
	@Override
	public Set<RobotDto> listAllRobots() {
		try {
			Game game = GameFactory.getCurrentGame();
			Set<RobotDto> robots = new HashSet<RobotDto>();
			// Make each player's robots into dto
			for (Player p : game.getPlayers()) {
				for (Robot r : p.getRobots()) {
					RobotDto rd = new RobotDto();
					rd.setKey(KeyFactory.keyToString(r.getKey()));
					rd.setLatitude(r.getLatitude());
					rd.setLongitude(r.getLongitude());
					rd.setOwner(p.getUser());  
					rd.setMovable(r.isMovable());
					robots.add(rd);
				}
			}
			return robots;
		} catch (NoActiveGameException e) {
			return null;
		}    
	}

	@Override
	public Map<String, Integer> listScores() {
		try {
			Game game = GameFactory.getCurrentGame();
			Map<String,Integer> scores = new HashMap<String,Integer>();
			for (Player p : game.getPlayers()) {
				scores.put(p.getUser(), p.getScore());
			}
			return scores;
		} catch (NoActiveGameException e) {
			return null;
		}   
	}

}
